package com.gec.hawaste.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.hawaste.entity.Statute;
import com.gec.hawaste.service.IStatuteService;
import com.gec.hawaste.utils.PageInfo;
import com.gec.hawaste.utils.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenxys
 * @since 2022-07-01
 */
@Slf4j
@RestController
@RequestMapping("/manager/statute")
public class StatuteController {
    @Autowired
    private IStatuteService statuteService;

    /**
     * 法律法规分页查询
     * @author chenxys
     * @param current 当前页
     * @param size 每页显示条数
     * @param type 类型
     * @return com.gec.hawaste.utils.ResultBean<com.gec.hawaste.utils.PageInfo>
     */
    @GetMapping("/select/{current}/{size}")
    public ResultBean<Page> select(@PathVariable("current") Long current,
                                   @PathVariable("size") Long size,
                                   @RequestParam(required = false) Long type){
        PageInfo<Statute> pageInfo = (PageInfo<Statute>) statuteService.selectByCondition(new PageInfo<>(current,size),type);

        pageInfo.setNavigatePage();

        log.info("法律法规分页查询");
        return ResultBean.ok(pageInfo);
    }

    /**
     * 根据ID删除法律法规
     * @author chenxys
     * @param id 法律法规ID
     * @return com.gec.hawaste.utils.ResultBean
     */
    @GetMapping("/delete")
    public ResultBean delete(Long id) {
        statuteService.removeById(id);
        log.info("删除法规");
        return ResultBean.ok();
    }

    /**
     * 查询单个法律法规
     * @author chenxys
     * @param id 法律法规ID
     * @return com.gec.hawaste.utils.ResultBean<com.gec.hawaste.entity.Statute>
     */
    @GetMapping("/selectOne")
    public ResultBean<Statute> selectOne(Long id){
        Statute statute = statuteService.getById(id);
        log.info("查询单个法律法规");
        return ResultBean.ok(statute);
    }

    /**
     * 保存或修改法律法规
     * @author chenxys
     * @param statute 法律法规
     * @return com.gec.hawaste.utils.ResultBean
     */
    @PostMapping("/saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody Statute statute){
        statuteService.saveOrUpdate(statute);
        log.info("保存或更新法规");
        return ResultBean.ok();
    }
}
